package com.main;

import java.util.ArrayList;
import com.restfb.Connection;
import com.restfb.types.Post;
import com.restfb.FacebookClient;

import com.main.Attendee;

//Implements methods:
//ArrayList<Attendee> getAttendees()
//Attendee getAttendeeByID(String aID)
//Attendee getAttendeeByName(String aName) 
//String toString() 

public class AttendeeList {

	// The list of attendees for event
	private ArrayList<Attendee> attendees;

	/**
	 * @param attendees
	 */
	// Constructor which should initialize the attendees arraylist.
	// Attendee class is used for each element of the array list, because the
	// connection returns the array list of Post type
	// public AttendeeList(Connection<Post> u_attendee_connection) {
	public AttendeeList(FacebookClient facebookClient, String u_eID) {
		String connection = u_eID + "/attending";
		Connection<Post> u_attendee_connection = facebookClient.fetchConnection(connection,Post.class);

		this.attendees = new ArrayList<Attendee>();
		Attendee attendee;
		// check connection exists
		if (u_attendee_connection != null) {
			for (int i = 0; i < u_attendee_connection.getData().size(); i++) {
				attendee = new Attendee(u_attendee_connection.getData().get(i)
						.getId(), u_attendee_connection.getData().get(i)
						.getName());
				System.out.println(attendee.toString());
				this.attendees.add(attendee);
			}
			System.out.println(attendees.toString());
		} else {
			System.out
					.println("Please, provide correct connection to eventID/attending");
		}

	}

	// Get method for array list of attendees
	public ArrayList<Attendee> getAttendees() {
		return attendees;
	}
	// Get number of attendeed
	public int NumberOfAttendees() {
		return attendees.size();
	}

	// Get method: Returns the Attendee by ID
	public Attendee getAttendeeByID(String aID) {
		if (aID != null) {
			for (Attendee attednee : attendees) {
				if (aID.equals(attednee.getID())) {
					return attednee;
				} else {
					System.out.println("No attendee with ID " + aID
							+ " is found");
					return null;
				}
			}
		}
		return null;
	}

	// Get method: Returns the Attendee by Name
	public Attendee getAttendeeByName(String aName) {
		if (aName != null) {
			for (Attendee attednee : attendees) {
				if (aName.equals(attednee.getName())) {
					return attednee;
				} else {
					System.out.println("No attendee with name " + aName
							+ " is found");
					return null;
				}
			}
		}
		return null;
	}

	@Override
	public String toString() {
		return "AttendeeList [attendees=" + attendees + "]";
	}

}
